package com.ddy.test.array;

/**
 * Created by ddy on 2019/9/5 1:28 PM
 * https://github.com/gouthampradhan/leetcode/blob/master/problems/src/two_pointers/MoveZeroes.java
 *  0: 0 0 0 0 1 0 1 0 2
 *  1: 0 0 0 0 1 0 1 0 2
 *  2: 0 0 0 0 1 0 1 0 2
 *  3: 0 0 0 0 1 0 1 0 2
 *  4: 1 0 0 0 0 0 1 0 2
 *  5: 1 0 0 0 0 0 1 0 2
 *  6: 1 1 0 0 0 0 0 0 2
 *  7: 1 1 0 0 0 0 0 0 2
 *  8: 1 1 2 0 0 0 0 0 0
 * ============================
 *  9: 1 1 2 0 0 0 0 0 0
 * 10: 1 1 2 0 0 0 0 0 0
 * 11: 1 1 2 0 0 0 0 0 0
 * 12: 1 1 2 0 0 0 0 0 0
 * 13: 1 1 2 0 0 0 0 0 0
 * 14: 1 1 2 0 0 0 0 0 0
 */

public class MoveZeroes {
    public static void main(String[] args) throws Exception {
        int[] nums = {0, 0, 0, 0, 1, 0, 1, 0, 2};
        new MoveZeroes().moveZeroes(nums);
        //for (int n : nums) System.out.print(n);
    }

    private void printArray(int step, int[] array){
        System.out.print(String.format("%2d", step) + ": ");
        for (int anArray : array) {
            System.out.print(anArray + " ");
        }
        System.out.println();
    }

    private void moveZeroes(int[] nums) {
        int i = 0;
        int step = 0;
        for (int j = 0, l = nums.length; j < l; ) {
            if (nums[j] == 0) j++;
            else {
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
                i++;
                j++;
            }
            printArray(step++, nums);
        }
        System.out.println("============================");
        while (i < nums.length){
            printArray(step++, nums);
            nums[i++] = 0;
        }
    }

}